package com.lagou.products.config;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * @description: Kafka配置类
 **/
@Configuration
public class KafkaConfig {

    @Bean
    public KafkaProducer kafkaProducer(){
        Properties properties = new Properties();
        //配置端口信息
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.110.20:9092,192.168.110.20:9093,192.168.110.20:9094");
        //序列化
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        //producer只要收到一个分区副本成功写入的通知就认为推送消息成功了。
        // 这里有一个地方需要注意，这个副本必须是leader副本。
        // 只有leader副本成功写入了，producer才会认为消息发送成功。
        properties.put(ProducerConfig.ACKS_CONFIG,"1");
        KafkaProducer<String, String> kafkaProducer = new KafkaProducer<String, String>(properties);
        return kafkaProducer;
    }
}
